<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sayHello
 *
 * @author sonia
 */
class actualizaNumPolizasTask extends sfBaseTask
{
  public function configure()
  {
    $this->namespace = 'actualiza';
    $this->name      = 'numPolizas';
    
    $this->addArgument('min', sfCommandArgument::OPTIONAL, 'Id minimo para la consulta', 0);
    $this->addArgument('max', sfCommandArgument::OPTIONAL, 'Id maximo para la consulta', 1000000);
    $this->addOptions(array(
    new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
    new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
        ));
  }
  
  public function execute($arguments = array(), $options = array())
  {      
      $databaseManager = new sfDatabaseManager($this->configuration);
      //Obtenemos todos los contactos
      $contactos = Doctrine_Query::create()
                        ->from('Contacto c1')
                        ->where('c1.id >= ?',$arguments['min'])
                        ->andWhere('c1.id < ?',$arguments['max'])
                        ->execute();                                
      
     foreach ($contactos as $contacto){
         //Cojo todas las polizas de ese contacto.
         $polizas = $contacto->getPolizas();
         $numPolizas = 0;
         $numBajas = 0;
         foreach ($polizas as $poliza){
             $numPolizas++;
             if ($poliza->getEstado() == "BAJA"){
                 $numBajas++;
             }
         }
         $contacto->setPolizasTotales($numPolizas);
         $contacto->setPolizasBajas($numBajas);
         $contacto->save(); 
      } 
  }  
}

?>
